/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IWhereResult;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 where' command.
  * 
  * @version $Revision: 33 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Where extends AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the result of the operation. */
	private List result = new ArrayList();

	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Convenience method for creating instances of Where.
	  * 
	  * @return the new Where instance.
	  */
	public static final Where newInstance()
	{
		MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null); 
		return new Where(status);
	}

	// ------------------------------------------------------------------------ Private Constructors

	/** Constructor for Where.
	  * 
	  * @param status the MultiStatus object.
	  */
	private Where(MultiStatus status)
	{
		super(status);
	}	
	
	// -------------------------------------------------------- Public Methods (IClientUserDelegate)
	
	/** @see IClientUserDelegate#outputInfo(char, String) */
	public void outputInfo(char level, String infoString)
	{
		if (level != '0')
		{
			return;
		}
			
		int firstStart = 0;
		int secondStart = infoString.indexOf("//", firstStart + 2);                    //$NON-NLS-1$
		int thirdStart = calculateThirdStart(infoString);		
			
		Result whereResult = new Result();
		whereResult.setDepotFilename(infoString.substring(firstStart, secondStart - 1));
		whereResult.setClientFilename(infoString.substring(secondStart, thirdStart - 1));
		whereResult.setLocalFilename(infoString.substring(thirdStart));

		result.add(whereResult);
	}
	
	// ----------------------------------------------------------- Public Methods (IPerforceCommand)
	
	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "where";                                                                //$NON-NLS-1$
	}

	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return getMultiStatus();
	}	
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return true;
	}	
	
	// ------------------------------------------------------------------------------ Public Methods
	
	/** Returns the result of the operation.
	  * 
	  * @return array of result objects.
	  */
	public IWhereResult[] getResult()
	{
		return (IWhereResult[]) result.toArray(new IWhereResult[result.size()]);
	}

	// ----------------------------------------------------------------------------- Private Methods
	
	/** Calculates the position of the third (local) path. This is a little 
	  * tricky as we have to deal with "null" client roots.
	  * 
	  * @param infoString the original infoString.
	  * @return the calculated position.
	  */
	private int calculateThirdStart(String infoString)
	{		
		List positions = new ArrayList();
		int pos = infoString.lastIndexOf("//");                                        //$NON-NLS-1$
		while ((pos = infoString.indexOf(' ', pos + 1)) != -1)
		{
			positions.add(new Integer(pos));
		}
		
		Integer result = (Integer) positions.get(positions.size() / 2);	
		return result.intValue() + 1;
	}

	// ---------------------------------------------------------------------- Private Nested Classes
	
	/**
	  * Implementation of the IWhereResult interface. Stores information
	  * provided by the 'p4 where' command. 
	  * 
	  * @see IWhereResult
	  */
	private static class Result implements IWhereResult
	{
		// ----------------------------------------------------------------------- Private Variables
		
		/** Stores the filename in depot syntax. */
		private String depotFilename;
		/** Stores the filename in client syntax. */
		private String clientFilename;
		/** Stores the filename in local syntax. */
		private String localFilename;
	
		// -------------------------------------------------------------------------- Public Methods
		
		/** @see IWhereResult#getDepotFilename() */
		public String getDepotFilename() 
		{ 
			return depotFilename; 
		}
	
		/** @see IWhereResult#getClientFilename() */
		public String getClientFilename() 
		{ 
			return clientFilename; 
		}
	
		/** @see IWhereResult#getLocalFilename() */
		public String getLocalFilename() 
		{ 
			return localFilename; 
		}
		
		// --------------------------------------------------------------------------------- Methods

		/** Sets the clientFilename.
		  * 
		  * @param clientFilename The clientFilename to set
		  */
		 void setClientFilename(String clientFilename) 
		 {
			this.clientFilename = clientFilename;
		}

		/** Sets the depotFilename.
		  * 
		  * @param depotFilename The depotFilename to set
		  */
		void setDepotFilename(String depotFilename) 
		{
			this.depotFilename = depotFilename;
		}

		/** Sets the localFilename.
		  * 
		  * @param localFilename The localFilename to set
		  */
		void setLocalFilename(String localFilename) 
		{
			this.localFilename = localFilename;
		}
	}	
}
